package cn.ibizlab.businesscentral.core.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.businesscentral.core.dto.*;
import cn.ibizlab.businesscentral.core.mapping.*;
import cn.ibizlab.businesscentral.core.odoo_crm.domain.Crm_partner_binding;
import cn.ibizlab.businesscentral.core.odoo_crm.service.ICrm_partner_bindingService;
import cn.ibizlab.businesscentral.core.odoo_crm.filter.Crm_partner_bindingSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"在CRM向导中处理业务伙伴的绑定或生成。" })
@RestController("Core-crm_partner_binding")
@RequestMapping("")
public class Crm_partner_bindingResource {

    @Autowired
    public ICrm_partner_bindingService crm_partner_bindingService;

    @Autowired
    @Lazy
    public Crm_partner_bindingMapping crm_partner_bindingMapping;

    @PreAuthorize("hasPermission(this.crm_partner_bindingMapping.toDomain(#crm_partner_bindingdto),'iBizBusinessCentral-Crm_partner_binding-Create')")
    @ApiOperation(value = "新建在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "新建在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.POST, value = "/crm_partner_bindings")
    public ResponseEntity<Crm_partner_bindingDTO> create(@Validated @RequestBody Crm_partner_bindingDTO crm_partner_bindingdto) {
        Crm_partner_binding domain = crm_partner_bindingMapping.toDomain(crm_partner_bindingdto);
		crm_partner_bindingService.create(domain);
        Crm_partner_bindingDTO dto = crm_partner_bindingMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingMapping.toDomain(#crm_partner_bindingdtos),'iBizBusinessCentral-Crm_partner_binding-Create')")
    @ApiOperation(value = "批量新建在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "批量新建在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.POST, value = "/crm_partner_bindings/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Crm_partner_bindingDTO> crm_partner_bindingdtos) {
        crm_partner_bindingService.createBatch(crm_partner_bindingMapping.toDomain(crm_partner_bindingdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "crm_partner_binding" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.crm_partner_bindingService.get(#crm_partner_binding_id),'iBizBusinessCentral-Crm_partner_binding-Update')")
    @ApiOperation(value = "更新在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "更新在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.PUT, value = "/crm_partner_bindings/{crm_partner_binding_id}")
    public ResponseEntity<Crm_partner_bindingDTO> update(@PathVariable("crm_partner_binding_id") Long crm_partner_binding_id, @RequestBody Crm_partner_bindingDTO crm_partner_bindingdto) {
		Crm_partner_binding domain  = crm_partner_bindingMapping.toDomain(crm_partner_bindingdto);
        domain .setId(crm_partner_binding_id);
		crm_partner_bindingService.update(domain );
		Crm_partner_bindingDTO dto = crm_partner_bindingMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingService.getCrmPartnerBindingByEntities(this.crm_partner_bindingMapping.toDomain(#crm_partner_bindingdtos)),'iBizBusinessCentral-Crm_partner_binding-Update')")
    @ApiOperation(value = "批量更新在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "批量更新在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.PUT, value = "/crm_partner_bindings/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Crm_partner_bindingDTO> crm_partner_bindingdtos) {
        crm_partner_bindingService.updateBatch(crm_partner_bindingMapping.toDomain(crm_partner_bindingdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingService.get(#crm_partner_binding_id),'iBizBusinessCentral-Crm_partner_binding-Remove')")
    @ApiOperation(value = "删除在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "删除在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.DELETE, value = "/crm_partner_bindings/{crm_partner_binding_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("crm_partner_binding_id") Long crm_partner_binding_id) {
         return ResponseEntity.status(HttpStatus.OK).body(crm_partner_bindingService.remove(crm_partner_binding_id));
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingService.getCrmPartnerBindingByIds(#ids),'iBizBusinessCentral-Crm_partner_binding-Remove')")
    @ApiOperation(value = "批量删除在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "批量删除在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.DELETE, value = "/crm_partner_bindings/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<Long> ids) {
        crm_partner_bindingService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.crm_partner_bindingMapping.toDomain(returnObject.body),'iBizBusinessCentral-Crm_partner_binding-Get')")
    @ApiOperation(value = "获取在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "获取在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.GET, value = "/crm_partner_bindings/{crm_partner_binding_id}")
    public ResponseEntity<Crm_partner_bindingDTO> get(@PathVariable("crm_partner_binding_id") Long crm_partner_binding_id) {
        Crm_partner_binding domain = crm_partner_bindingService.get(crm_partner_binding_id);
        Crm_partner_bindingDTO dto = crm_partner_bindingMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取在CRM向导中处理业务伙伴的绑定或生成。草稿", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "获取在CRM向导中处理业务伙伴的绑定或生成。草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/crm_partner_bindings/getdraft")
    public ResponseEntity<Crm_partner_bindingDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(crm_partner_bindingMapping.toDto(crm_partner_bindingService.getDraft(new Crm_partner_binding())));
    }

    @ApiOperation(value = "检查在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "检查在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.POST, value = "/crm_partner_bindings/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody Crm_partner_bindingDTO crm_partner_bindingdto) {
        return  ResponseEntity.status(HttpStatus.OK).body(crm_partner_bindingService.checkKey(crm_partner_bindingMapping.toDomain(crm_partner_bindingdto)));
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingMapping.toDomain(#crm_partner_bindingdto),'iBizBusinessCentral-Crm_partner_binding-Save')")
    @ApiOperation(value = "保存在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "保存在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.POST, value = "/crm_partner_bindings/save")
    public ResponseEntity<Boolean> save(@RequestBody Crm_partner_bindingDTO crm_partner_bindingdto) {
        return ResponseEntity.status(HttpStatus.OK).body(crm_partner_bindingService.save(crm_partner_bindingMapping.toDomain(crm_partner_bindingdto)));
    }

    @PreAuthorize("hasPermission(this.crm_partner_bindingMapping.toDomain(#crm_partner_bindingdtos),'iBizBusinessCentral-Crm_partner_binding-Save')")
    @ApiOperation(value = "批量保存在CRM向导中处理业务伙伴的绑定或生成。", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" },  notes = "批量保存在CRM向导中处理业务伙伴的绑定或生成。")
	@RequestMapping(method = RequestMethod.POST, value = "/crm_partner_bindings/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<Crm_partner_bindingDTO> crm_partner_bindingdtos) {
        crm_partner_bindingService.saveBatch(crm_partner_bindingMapping.toDomain(crm_partner_bindingdtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizBusinessCentral-Crm_partner_binding-searchDefault-all') and hasPermission(#context,'iBizBusinessCentral-Crm_partner_binding-Get')")
	@ApiOperation(value = "获取数据集", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" } ,notes = "获取数据集")
    @RequestMapping(method= RequestMethod.GET , value="/crm_partner_bindings/fetchdefault")
	public ResponseEntity<List<Crm_partner_bindingDTO>> fetchDefault(Crm_partner_bindingSearchContext context) {
        Page<Crm_partner_binding> domains = crm_partner_bindingService.searchDefault(context) ;
        List<Crm_partner_bindingDTO> list = crm_partner_bindingMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizBusinessCentral-Crm_partner_binding-searchDefault-all') and hasPermission(#context,'iBizBusinessCentral-Crm_partner_binding-Get')")
	@ApiOperation(value = "查询数据集", tags = {"在CRM向导中处理业务伙伴的绑定或生成。" } ,notes = "查询数据集")
    @RequestMapping(method= RequestMethod.POST , value="/crm_partner_bindings/searchdefault")
	public ResponseEntity<Page<Crm_partner_bindingDTO>> searchDefault(@RequestBody Crm_partner_bindingSearchContext context) {
        Page<Crm_partner_binding> domains = crm_partner_bindingService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(crm_partner_bindingMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


}

